#include <sstream>

#include "utility.hpp"

#include "cmd_err_code.hpp"

#include "confsvr_cmd.hpp"

namespace cmd
{
	void Host::serialize(Streamer & streamer)
	{
		streamer & _ip
			     & _port;
	}

	const std::string Host::to_string() const
	{
		std::ostringstream oss;
		oss << "{ip: " << _ip
			<< ", port: " << _port
			<< "}";
		return oss.str();
	}

	void AreaConf::serialize(Streamer & streamer)
	{
		streamer & _area_id
				 & _room_num
				 & _table_num;
	}

	const std::string AreaConf::to_string() const
	{
		std::ostringstream oss;
		oss << "{area_id: " << (int)_area_id
			<< ", room_num: " << (int)_room_num
			<< ", table_num: " << (int)_table_num
			<< "}";
		return oss.str();
	}

	void CommonConf::serialize(Streamer & streamer)
	{
		streamer & _fixed_page_size
			     & _fixed_capacity_size
				 & _max_connection
				 & _epoll_timeout;
	}

	const std::string CommonConf::to_string() const
	{
		std::ostringstream oss;
		oss << "{fixed_page_size: " << _fixed_page_size
			<< ", fixed_capacity_size: " << _fixed_capacity_size
			<< ", max_connection: " << _max_connection
			<< ", epoll_timeout: " << _epoll_timeout
			<< "}";
		return oss.str();
	}

	void SSDBSvrRes::serialize(Streamer & streamer)
	{
		SSHead::serialize(streamer);
		streamer & _err_code;
		
		if (0 != _err_code)
		{
			return;
		}

		streamer & _common
			     & _thread_num
				 & _thread_queue_size
				 & _thread_page_size
				 & _thread_capacity_size
				 & _service
				 & _mysql_host
				 & _mysql_user
				 & _mysql_passwd
				 & _mysql_dbname;
	}

	const std::string SSDBSvrRes::to_string() const
	{
		std::ostringstream oss;

		oss << SSHead::to_string()
			<< " body: {err_code: " << (int)_err_code << "(" << game_err_to_str(_err_code) << ")";

		if (0 != _err_code)
		{
			oss << "}";
			return oss.str();
		}

		oss << ", common: " << _common.to_string()
			<< ", thrad_num: " << (int)_thread_num
			<< ", thread_queue_size: " << (int)_thread_queue_size
			<< ", thrad_page_size: " << (int)_thread_page_size
			<< ", thread_capacity_size: " << (int)_thread_capacity_size
			<< ", service: " << _service.to_string()
			<< ", mysql_host: " << _mysql_host
			<< ", mysql_user: " << _mysql_user
			<< ", mysql_passwd: " << _mysql_passwd
			<< ", msyql_dbname: " << _mysql_dbname
			<< "}";
		return oss.str();
	}

	void SSBalanceRes::serialize(Streamer & streamer)
	{
		SSHead::serialize(streamer);

		streamer & _err_code;
		
		if (0 != _err_code)
		{
			return;
		}

		streamer & _common
			     & _service;
	}

	const std::string SSBalanceRes::to_string() const
	{
		std::ostringstream oss;

		oss << SSHead::to_string()
			<< " body: {err_code: " << (int)_err_code << "(" << game_err_to_str(_err_code) << ")";
			
		if (0 != _err_code)
		{
			oss << "}";
			return oss.str();
		}

		oss << ", common: " << _common.to_string()
			<< ", service: " << _service.to_string()
			<< "}";
		return oss.str();
	}
	
	void SSAccountRes::serialize(Streamer & streamer)
	{
		SSHead::serialize(streamer);
		streamer & _err_code;
		
		if (0 != _err_code)
		{
			return;
		}

		streamer & _common
			     & _service
				 & _dbsvr;
	}

	const std::string SSAccountRes::to_string() const
	{
		std::ostringstream oss;

		oss << SSHead::to_string()
			<< " body: {err_code: " << (int)_err_code << "(" << game_err_to_str(_err_code) << ")";

		if (0 != _err_code)
		{
			oss << "}";
			return oss.str();
		}

		oss << ", common: " << _common.to_string()
			<< ", service: " << _service.to_string()
			<< ", dbsvr: " << _dbsvr.to_string()
			<< "}";
		return oss.str();
	}

	const std::string SSHttpClientRes::to_string() const
	{
		std::ostringstream oss;

		oss << SSHead::to_string()
			<< " body: {err_code: " << (int)_err_code << "(" << game_err_to_str(_err_code) << ")";

		if (0 != _err_code)
		{
			oss << "}";
			return oss.str();
		}

		oss << ", common: " << _common.to_string()
			<< ", service: " << _service.to_string()
			<< ", url: " << _url
			<< ", version: " << _version
			<< ", appid: " << _appid
			<< ", appkey: " << _appkey
			<< ", pf: " << _pf
			<< ", format: " << _format
			<< ", http_timeout: " << _http_timeout
			<< ", thread_num: " << _thread_num
			<< "}";
		return oss.str();
	}

	void SSHttpClientRes::serialize(Streamer & streamer)
	{
		SSHead::serialize(streamer);
		streamer & _err_code;
		
		if (0 != _err_code)
		{
			return;
		}

		streamer & _common
			     & _service
				 & _url
				 & _version
				 & _appid
				 & _appkey
				 & _pf
				 & _format
				 & _http_timeout
				 & _thread_num;
	}

	void SSGatewayRes::serialize(Streamer & streamer)
	{
		SSHead::serialize(streamer);
		streamer & _err_code;
		
		if (0 != _err_code)
		{
			return;
		}

		streamer & _timeout
			     & _common
			     & _scence
				 & _client
				 & _http_client
				 & _dbsvr
				 & _balance;
	}

	const std::string SSGatewayRes::to_string() const
	{
		std::ostringstream oss;

		oss << SSHead::to_string()
			<< " body: {err_code: " << (int)_err_code << "(" << game_err_to_str(_err_code) << ")";

		if (0 != _err_code)
		{
			oss << "}";
			return oss.str();
		}

		oss << "timeout: " << _timeout
			<< ", common: " << _common.to_string()
			<< ", scence: " << _scence.to_string()
			<< ", client: " << _client.to_string()
			<< ", httpclient: " << _http_client.to_string()
			<< ", account: " << _dbsvr.to_string()
			<< ", balance: " << _balance.to_string()
			<< "}";
		return oss.str();
	}

	void SSScenceRes::serialize(Streamer & streamer)
	{
		SSHead::serialize(streamer);
		streamer & _err_code;
		
		if (0 != _err_code)
		{
			return;
		}

		streamer & _common
				 & _gateway_list
				 & _area_list;
	}

	const std::string SSScenceRes::to_string() const
	{
		std::ostringstream oss;

		oss << SSHead::to_string()
			<< " body: {err_code: " << (int)_err_code << "(" << game_err_to_str(_err_code) << ")";

		if (0 != _err_code)
		{
			oss << "}";
			return oss.str();
		}

		oss << ", common: " << _common.to_string()
			<< ", gateway_list: " << Utility::vec_to_string(_gateway_list)
			<< ", area_list: " << Utility::vec_to_string(_area_list)
			<< "}";
		return oss.str();
	}
};

